package com.hhl.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhl.flower.mapper.FlowerMapper;
import com.hhl.flower.mapper.TypeMapper;
import com.hhl.flower.pojo.entity.Flower;
import com.hhl.flower.pojo.entity.Type;
import com.hhl.flower.pojo.vo.FlowerVO;
import com.hhl.flower.service.FlowerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Liubuzhu
* @description 针对表【flower】的数据库操作Service实现
* @createDate 2024-06-05 20:42:43
*/
@Service
public class FlowerServiceImpl extends ServiceImpl<FlowerMapper, Flower>
    implements FlowerService{

	@Autowired
	private FlowerMapper flowermapper;

	@Autowired
	private TypeMapper typeMapper;

	@Override
	public List<FlowerVO> getFowerList() {
		QueryWrapper<Flower> queryWrapper = new QueryWrapper<>();
		List<Flower> flowerList = flowermapper.selectList(queryWrapper);

		List<FlowerVO> flowerVOS = new ArrayList<>();
		for (Flower flower : flowerList) {
			QueryWrapper<Type> queryWrapper2 = new QueryWrapper<>();
			queryWrapper2.eq("id",flower.getTId());
            Type type = typeMapper.selectOne(queryWrapper2);
			FlowerVO flowerVO = new FlowerVO();
			BeanUtils.copyProperties(flower, flowerVO);
			flowerVO.setTName(type.getName());
			flowerVOS.add(flowerVO);
		}

		return flowerVOS;
	}

	public FlowerVO getByFlowerId(Integer fid) {
		Flower flower = flowermapper.selectById(fid);
		QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id",flower.getTId());
        Type type = typeMapper.selectOne(queryWrapper);
        FlowerVO flowerVO = new FlowerVO();
        BeanUtils.copyProperties(flower, flowerVO);
		flowerVO.setTName(type.getName());

        return flowerVO;
	}

	public List<FlowerVO> getByNameLike(String keyword) {
		QueryWrapper<Flower> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",keyword);
        List<Flower> flowerList = flowermapper.selectList(queryWrapper);

        List<FlowerVO> flowerVOS = new ArrayList<>();
        for (Flower flower : flowerList) {
            QueryWrapper<Type> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("id",flower.getTId());
            Type type = typeMapper.selectOne(queryWrapper2);
            FlowerVO flowerVO = new FlowerVO();
            BeanUtils.copyProperties(flower, flowerVO);
            flowerVO.setTName(type.getName());
            flowerVOS.add(flowerVO);
        }

        return flowerVOS;
	}

	public List<FlowerVO> getByType(Integer tid) {
		QueryWrapper<Flower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t_id",tid);
        List<Flower> flowerList = flowermapper.selectList(queryWrapper);

        List<FlowerVO> flowerVOS = new ArrayList<>();
        for (Flower flower : flowerList) {
            QueryWrapper<Type> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("id",flower.getTId());
            Type type = typeMapper.selectOne(queryWrapper2);
            FlowerVO flowerVO = new FlowerVO();
            BeanUtils.copyProperties(flower, flowerVO);
            flowerVO.setTName(type.getName());
            flowerVOS.add(flowerVO);
        }

        return flowerVOS;
	}

	public void addViews(Integer fid) {
		Flower flower = flowermapper.selectById(fid);
        flower.setViews(flower.getViews()+1);
        flowermapper.updateById(flower);
	}
}




